﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;


namespace Capstone_project
{
    public partial class frmAddNewProject : Form
    {
        public frmAddNewProject()
        {
            InitializeComponent();
        }
        
        DataSet ds1 = Common.ds;
        DataSet ds2 = Common.ds;
        SqlConnection con = Common.con;
        SqlCommand com1,com2;
        SqlDataAdapter adap1,adap2;
        private void frmAddNewProject_Load(object sender, EventArgs e)
        {
            if(con.State!=ConnectionState.Open)
                con.Open();
            string sql = "SELECT * FROM CUSTOMER";
            com1 = new SqlCommand(sql, con);
            adap1 = new SqlDataAdapter(com1);
            ds1 = new DataSet();
            adap1.Fill(ds1, "CUSTOMER");
            DataTable dt1 = ds1.Tables["CUSTOMER"];

            string sql1 = "SELECT * FROM PROJECT_STATUS";
            com2 = new SqlCommand(sql1, con);
            adap2 = new SqlDataAdapter(com2);
            ds2 = new DataSet();
            adap2.Fill(ds2, "PROJECT_STATUS");
            DataTable dt2 = ds2.Tables["PROJECT_STATUS"];

            con.Close();
            cmbCustomer.DataSource = dt1;
            cmbCustomer.DisplayMember = "NAME_CUSTOMER";
            cmbCustomer.ValueMember = "ID_CUSTOMER";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtIdProject.Text == "" || txtProjectName.Text == "" || dtpStartDate.Text=="" || dtpEndDate.Text==""|| cmbCustomer.Text=="")
                {
                    MessageBox.Show("You must enter all infomation");
                }
                else
                {
                    if (Common.CheckExistProject(txtIdProject.Text.Trim()) == false)
                    {
                        MessageBox.Show("This IDProject is existed, please change other id.");
                        txtIdProject.Focus();
                        txtIdProject.BackColor = Color.Red;
                    }
                    else
                    {
                        if (dtpEndDate.Value <= dtpStartDate.Value)
                        {
                            MessageBox.Show("End Date must bigger than start date !!!!");
                        }
                        else
                        {
                            frmCreateNewTaskgroup.idproject = txtIdProject.Text;
                            frmCreateNewTaskgroup.nameproject = txtProjectName.Text;

                            string sql = "INSERT INTO PROJECT(ID_PROJECT,NAME_PROJECT,ID_CUSTOMER,ID_STATUS,START_DATE,END_DATE)VALUES('" + txtIdProject.Text + "','" + txtProjectName.Text + "','" + cmbCustomer.SelectedValue + "','PST001','" + dtpStartDate.Text + "','" + dtpEndDate.Text + "')";
                            if (con.State != ConnectionState.Open)
                                con.Open();
                            SqlCommand cmd = new SqlCommand(sql, con);
                            int kq = cmd.ExecuteNonQuery();
                            if (kq != 0)
                            {
                                frmNextStep nextstep = new frmNextStep();
                                if (nextstep.ShowDialog() == DialogResult.OK)
                                {
                                    this.Close();

                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("Have Problem in proccess");
            }

        }

        private void txtIdProject_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((e.KeyChar > (char)47) && (e.KeyChar < (char)58)) || ((e.KeyChar > (char)64) && (e.KeyChar < (char)91)) || ((e.KeyChar > (char)96) && (e.KeyChar < (char)123)) || (e.KeyChar == (char)45) || (e.KeyChar == (char)95) || (e.KeyChar == (char)8))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

       
       
    }
}
